import {callApi} from '../../util/callApi';

/**
 * 获取分类列表
 * @returns {function()}
 */
const getList = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/term/list';
    return callApi.get(request_url,formdata).then(res=>{
      if(res.code === 0){
        return Promise.resolve(dispatch({
          type: 'TERM_LIST',
          termlist : res.data
        }))
      }else{
        return Promise.reject(res)
      }
    });
  }
}

/**
 * 添加分类
 * @param formdata
 * @returns {function()}
 */
const postAddTerm = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/term/add';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      if(res.code === 0){
        return Promise.resolve(dispatch({
          type : 'TREM_ADD',
          info : res.data
        }));
      }else{
        return Promise.reject(res)
      }
    });
  }
}

/**
 * 编辑分类
 * @param formdata
 * @returns {function()}
 */
const postEditTerm = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/term/update';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      if(res.code === 0){
        return Promise.resolve(dispatch({
          type : 'TERM_EDIT',
          index : formdata.index,
          info : res.data
        }));
      }else{
        return Promise.reject(res)
      }
    });
  }
}

/**
 * 删除分类信息
 * @param formdata
 * @returns {function()}
 */
const postDeleteTerm = (formdata) => {
  return (dispatch) => {
    const request_url = '/admin/term/delete';
    return callApi.post(request_url,JSON.stringify(formdata)).then(res=>{
      return Promise.resolve(dispatch({
        type : 'TERM_DELETE',
        index : formdata.index
      }));
    });

  }
}

export default {
  getList,
  postAddTerm,
  postEditTerm,
  postDeleteTerm
}